import matplotlib.pyplot as plt
import numpy as np
import calculateradiation as radiation
import libraries.filereader as filereader
import shtRipper
import scipy.interpolate
import calibration.calibration as calibration


def to_float(array):
    return list(map(float, array))


def create_plot(file_name, x, y):
    print(x, y)
    plt.plot(x, y)
    plt.title("Zeff")
    plt.xlabel("time, ms")
    plt.ylabel("Zeff")

    plt.savefig(file_name)
    plt.show()


shtFile = 'sht41092.sht'

res = shtRipper.ripper.read(shtFile)

sxr_15_obj = res.get('SXR 15 мкм normal')
sxr_50_obj = res.get('SXR 50 mkm normal')
sxr_80_obj = res.get('SXR 80 mkm normal')
sxr_127_obj = res.get('SXR 127 мкм normal')

sxr_15 = scipy.interpolate.interp1d(sxr_15_obj['x'], sxr_15_obj['y'], fill_value='extrapolate')
sxr_50 = scipy.interpolate.interp1d(sxr_50_obj['x'], sxr_50_obj['y'], fill_value='extrapolate')
sxr_80 = scipy.interpolate.interp1d(sxr_80_obj['x'], sxr_80_obj['y'], fill_value='extrapolate')
sxr_127 = scipy.interpolate.interp1d(sxr_127_obj['x'], sxr_127_obj['y'], fill_value='extrapolate')

radiusArray = [
    -24,
    -23,
    -21,
    -18,
    -16,
    -13,
    -11,
    -9,
    -7,
    -5,
    0,
    5,
    7,
    9,
    11,
    13,
    16,
    18,
    21,
    23,
    24,
]

with open('ne-41092.txt', 'r') as file:
    lines = [" ".join(line.strip().split()).split(" ") for line in file]
    lines = list(map(to_float, lines))
    tomsonTime = []
    array24 = []
    array23 = []
    array21 = []
    array18 = []
    array16 = []
    array13 = []
    array11 = []
    array9 = []
    array7 = []
    array5 = []
    array0 = []
    for line in lines:
        tomsonTime.append(line[0])
        array0.append(line[1] / 10 ** 6),
        array24.append(line[2] / 10 ** 6)
        array23.append(line[3] / 10 ** 6)
        array21.append(line[4] / 10 ** 6)
        array18.append(line[5] / 10 ** 6)
        array16.append(line[6] / 10 ** 6)
        array13.append(line[7] / 10 ** 6)
        array11.append(line[8] / 10 ** 6)
        array9.append(line[9] / 10 ** 6)
        array7.append(line[10] / 10 ** 6)
        array5.append(line[11] / 10 ** 6)

    densityByTime = dict()
    for index, timeT in enumerate(tomsonTime):
        densityByTime[str(timeT)] = scipy.interpolate.interp1d(radiusArray, [
            array24[index],
            array23[index],
            array21[index],
            array18[index],
            array16[index],
            array13[index],
            array11[index],
            array9[index],
            array7[index],
            array5[index],
            array0[index],
            array5[index],
            array7[index],
            array9[index],
            array11[index],
            array13[index],
            array16[index],
            array18[index],
            array21[index],
            array23[index],
            array24[index],
        ], fill_value='extrapolate')

with open('te-41092.txt', 'r') as file:
    lines = [" ".join(line.strip().split()).split(" ") for line in file]
    lines = list(map(to_float, lines))
    tomsonTime = []
    array24 = []
    array23 = []
    array21 = []
    array18 = []
    array16 = []
    array13 = []
    array11 = []
    array9 = []
    array7 = []
    array5 = []
    array0 = []
    for line in lines:
        tomsonTime.append(line[0])
        array0.append(line[1])
        array24.append(line[2])
        array23.append(line[3])
        array21.append(line[4])
        array18.append(line[5])
        array16.append(line[6])
        array13.append(line[7])
        array11.append(line[8])
        array9.append(line[9])
        array7.append(line[10])
        array5.append(line[11])

    temperatureByTime = dict()
    for index, timeT in enumerate(tomsonTime):
        temperatureByTime[str(timeT)] = scipy.interpolate.interp1d(radiusArray, [
            array24[index],
            array23[index],
            array21[index],
            array18[index],
            array16[index],
            array13[index],
            array11[index],
            array9[index],
            array7[index],
            array5[index],
            array0[index],
            array5[index],
            array7[index],
            array9[index],
            array11[index],
            array13[index],
            array16[index],
            array18[index],
            array21[index],
            array23[index],
            array24[index],
        ], fill_value='extrapolate')

pffByTime = dict()
for key, value in densityByTime.items():
    if float(key) < 0.150:
        continue
    print(key)
    pffByTime[key] = radiation.calculateRadiation(temperatureByTime[key], value)


# pffByTime = {'0.152287': 0.2814303704826884, '0.15532': 0.29480449022298183, '0.158354': 0.4177830033859891,
#              '0.161387': 0.3775585020780932, '0.164422': 0.4580810030244474, '0.167455': 0.4849462858678067,
#              '0.170488': 0.23198952462801864, '0.173521': 0.31022247402698944, '0.176557': 0.20121631931455555,
#              '0.17959': 0.18960752080026996, '0.182623': 0.20953567302305756, '0.185656': 0.20561100347710695,
#              '0.188691': 0.16301573025499536, '0.191725': 0.17125209965253319, '0.19476': 0.18255910883664891,
#              '0.197793': 0.16682517671681524, '0.200826': 0.1815738068627821, '0.203859': 0.1220980336398113,
#              '0.206895': 0.1559475155917391, '0.209928': 0.13701420170078726, '0.212961': 0.11415233169824618,
#              '0.215994': 0.09633901252447519, '0.219029': 0.13240658498252758, '0.222063': 0.09049088764589974,
#              '0.225096': 0.046327297421380226}

ZeffByTime15 = dict()
ZeffByTime50 = dict()
ZeffByTime80 = dict()
ZeffByTime127 = dict()
calibration15 = calibration.calibration(15)
calibration50 = calibration.calibration(50)
calibration80 = calibration.calibration(80)
calibration127 = calibration.calibration(127)
for key, value in pffByTime.items():
    ZeffByTime15[key] = sxr_15(float(key)) / value * calibration15 * 1.11

    ZeffByTime50[key] = sxr_50(float(key)) / value * calibration50 * 0.94

    ZeffByTime80[key] = sxr_80(float(key)) / value * calibration80 * 1.55

    ZeffByTime127[key] = sxr_127(float(key)) / value * calibration127 * 0.55

create_plot('Zeff15.png', to_float(ZeffByTime15.keys()), list(ZeffByTime15.values()))
create_plot('Zeff50.png', to_float(ZeffByTime50.keys()), list(ZeffByTime50.values()))
create_plot('Zeff80.png', to_float(ZeffByTime80.keys()), list(ZeffByTime80.values()))
create_plot('Zeff127.png', to_float(ZeffByTime127.keys()), list(ZeffByTime127.values()))
